import Router from '@koa/router';
const router = new Router();
import projectServices from '../controllers/index.js';
import { verify } from '../utils/jwt/index.js';

router.prefix('/project');

// 获取项目列表
router.post('/list', verify(), async (ctx, next) => {
  try {
    const data = await projectServices.getProjectList(ctx.user_id);
    ctx.body = {
      code: 200,
      message: '获取项目列表成功',
      data,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 获取项目详情
router.post('/info', verify(), async (ctx, next) => {
  try {
    const { projectId } = ctx.request.body;
    const res = await projectServices.getProjectInfo(projectId);
    const data = res[0] || null;
    ctx.body = {
      code: 200,
      message: '获取项目详情成功',
      data,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 创建项目
router.post('/createProject', verify(), async (ctx, next) => {
  const { creator_id, name, description } = ctx.request.body;
  try {
    const res = await projectServices.addProjectApi(
      creator_id,
      name,
      description
    );

    ctx.body = {
      code: 200,
      message: '创建项目成功',
      data: res,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 更新项目
router.post('/updateInfo', verify(), async (ctx, next) => {
  try {
    const { projectId, name, description } = ctx.request.body;
    const res = await projectServices.updateProjectInfo(
      projectId,
      name,
      description
    );
    ctx.body = {
      code: 200,
      message: '更新成功',
      data: null,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 删除项目
router.post('/deleteProject', verify(), async (ctx, next) => {
  try {
    const { projectId } = ctx.request.body;
    const res = await projectServices.deleteProject(projectId);
    ctx.body = {
      code: 200,
      message: '项目及其相关数据已成功删除',
      data: null,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 获取项目模块树
router.post('/moduleTree', verify(), async (ctx, next) => {
  try {
    const { projectId } = ctx.request.body;
    const data = await projectServices.getProjectModuleTree(
      projectId,
      ctx.user_id
    );

    ctx.body = {
      code: 200,
      message: '获取项目模块树成功',
      data,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 获取接口详情
router.post('/getApiDetail', verify(), async (ctx, next) => {
  try {
    const { id } = ctx.request.body;
    const res = await projectServices.getApiDetail(id);
    const data = res[0];

    ctx.body = {
      code: 200,
      message: '获取成功',
      data,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 更新接口详情
router.post('/updateApiDetail', verify(), async (ctx, next) => {
  try {
    const { ...apiDetail } = ctx.request.body;
    const res = await projectServices.updateApiDetail(ctx.user_id, apiDetail);
    if (res) {
      ctx.body = {
        code: 200,
        message: '更新成功',
        data: { id: res[0].id },
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 创建接口
router.post('/createApiDeatial', verify(), async (ctx, next) => {
  try {
    const { ...apiDetail } = ctx.request.body;
    const res = await projectServices.createApiDetail(ctx.user_id, apiDetail);
    if (res) {
      ctx.body = {
        code: 200,
        message: '创建成功',
        data: { id: res[0].id },
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 删除接口
router.post('/deleteApi', verify(), async (ctx, next) => {
  const { id } = ctx.request.body;
  try {
    const res = await projectServices.deleteApi(id);
    if (res) {
      ctx.body = {
        code: 200,
        message: '删除成功',
        data: res,
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 获取项目顶级模块列表
router.post('/getTopLevelModules', verify(), async (ctx, next) => {
  const { projectId } = ctx.request.body;
  try {
    const res = await projectServices.getTopLevelModules(projectId);
    const data = res || null;
    ctx.body = {
      code: 200,
      message: '获取顶级项目模块列表成功',
      data,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 获取模块下的子模块
router.post('/getModuleChildren', verify(), async (ctx, next) => {
  const { projectId, id } = ctx.request.body;
  try {
    const res = await projectServices.getModuleChildren(projectId, id);
    const data = res || null;
    ctx.body = {
      code: 200,
      message: '获取项目子模块列表成功',
      data,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 根据id获取最短树的全部路径
router.post('/getShortestTreeById', verify(), async (ctx, next) => {
  const { id, projectId, type } = ctx.request.body;
  try {
    const res = await projectServices.getShortestTreeById(id, projectId, type);
    if (res) {
      ctx.body = {
        code: 200,
        message: '导航栏查询api节点成功',
        data: res,
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 新增模块
router.post('/AddModuleApi', verify(), async (ctx, next) => {
  const { project_id, parent_id, name } = ctx.request.body;
  try {
    const res = await projectServices.AddModuleApi(project_id, parent_id, name);
    if (res) {
      ctx.body = {
        code: 200,
        message: `新增模块成功`,
        data: {
          id: res.insertId,
        },
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 编辑模块
router.post('/editModuleApi', verify(), async (ctx, next) => {
  const { id, project_id, parent_id, name } = ctx.request.body;
  try {
    const res = await projectServices.editModuleApi(
      id,
      project_id,
      parent_id,
      name
    );

    if (res) {
      ctx.body = {
        code: 200,
        message: `更新成功`,
        data: null,
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 删除模块
router.post('/deleteModuleApi', verify(), async (ctx, next) => {
  const { id, projectId } = ctx.request.body;
  try {
    const res = await projectServices.deleteModuleApi(id, projectId);

    if (res) {
      ctx.body = {
        code: 200,
        message: `删除成功`,
        data: res.parentId,
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 搜索api
router.post('/searchApis', verify(), async (ctx, next) => {
  const { projectId, keyword } = ctx.request.body;

  try {
    const res = await projectServices.searchApis(projectId, keyword);
    if (res) {
      ctx.body = {
        code: 200,
        message: `搜索成功`,
        data: res,
      };
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

// 获取数据统计
router.post('/getDataStatistics', verify(), async (ctx, next) => {
  const { id } = ctx.request.body;
  try {
    const res = await projectServices.getDataStatistics(id);
    ctx.body = {
      code: 200,
      message: '获取数据成功',
      data: res[0],
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器内部错误',
      data: null,
    };
  }
});

export default router;
